/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.api.service.impl.DriverInfoServiceImpl
 * <p>
 * 类产生时间: 2014-11-14 16:34
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 项目组 : 苏州公安移动门户
 * <p>
 * 部门: 手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.api.entity.DriverInfoApi;
import com.oceansoft.mobile.econsole.modules.api.persistence.IDriverInfoApiMapper;
import com.oceansoft.mobile.econsole.modules.api.persistence.IDriverInfoMapper;
import com.oceansoft.mobile.econsole.modules.api.service.IDriverInfoApiService;
import com.oceansoft.mobile.econsole.modules.api.service.IDriverInfoService;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.message.entity.ExceptionCommonBase;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;
import com.oceansoft.mobile.econsole.modules.message.util.Utils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static org.springframework.util.StringUtils.hasText;

/**
 * 驾驶员信息业务组件处理类
 *
 * @author xujin
 * @time 2014-11-14 16:34
 */
@Service
@Transactional
public class DriverInfoApiServiceImpl implements IDriverInfoApiService {
    @Resource
    protected ConfigInfo configInfo;
    @Resource
    private IDriverInfoApiMapper driverInfoApiMapper;

    @Override
    public Result pushBindInfo(String sfzh, String mobile) {
        try {
            JSONObject json = new JSONObject();
            json.put("SFZH", sfzh);
            json.put("CL", driverInfoApiMapper.loadVehicleBindInfo(sfzh));
            json.put("JSZ", driverInfoApiMapper.loadDriverBindInfo(sfzh));

            //构造PushMessage
            PushMessage message = new PushMessage(500001835, 3, "pap_" + mobile, "{\"content_type\":3,\"extras\":\"\",\"title\":\"驾驶证信息\",\"message\":" + json.toJSONString() + "}", "自动推送");
            String resp = Utils.httpPost(configInfo.getPushServerHost(), null, Utils.toParamFromBean(message));
            return new Result(Result.SUCC, "驾驶证车辆数据推送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "驾驶证车辆数据推送失败");
        }
    }

    @Override
    public Result loadVehicleBindInfo(String sfzh) {
        if (StringUtils.isEmpty(sfzh)) {
            return new Result(Result.FAILURE, "请输入要查询的身份证号");
        }
        List<DriverInfoApi> list = driverInfoApiMapper.loadVehicleBindInfo(sfzh);
        if (list.isEmpty())
            return new Result(Result.FAILURE, "暂无绑定信息");
        return new Result(list);
    }

    @Override
    public Result loadDriveBindInfo(String sfzh) {
        if (StringUtils.isEmpty(sfzh)) {
            return new Result(Result.FAILURE, "请输入要查询的身份证号");
        }
        List<DriverInfoApi> list = driverInfoApiMapper.loadDriverBindInfo(sfzh);
        if (list.isEmpty())
            return new Result(Result.FAILURE, "暂无绑定信息");
        return new Result(list);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Result saveInfo(DriverInfoApi driverInfoApi) {
        if (StringUtils.isEmpty(driverInfoApi.getSfzh()))
            return new Result(Result.FAILURE, "身份证号不能为空");

        if (hasText(driverInfoApi.getLpn()) && !hasText(driverInfoApi.getVin()))
            return new Result(Result.FAILURE, "车架号不能为空");

        if (hasText(driverInfoApi.getVin()) && !hasText(driverInfoApi.getLpn()))
            return new Result(Result.FAILURE, "车牌号不能为空");

        if (hasText(driverInfoApi.getDrivingLicense()) && !hasText(driverInfoApi.getFn()))
            return new Result(Result.FAILURE, "档案编号不能为空");

        if (hasText(driverInfoApi.getFn()) && !hasText(driverInfoApi.getDrivingLicense()))
            return new Result(Result.FAILURE, "驾驶证号不能为空");

        //保存驾驶证记录
        driverInfoApiMapper.saveDriverInfo(driverInfoApi);
        if (driverInfoApi.getId() > 0)
            return new Result(Result.SUCC, Integer.valueOf(driverInfoApi.getId()), "信息保存成功");
        return new Result(Result.FAILURE, "信息保存失败");
    }

    @Override
    public Result saveInfoBySfzh(String sfzh) {
        try {
            String cl = String.format("http://10.35.252.127:8080/zhkclrytj/jdcxxcx/getClsxx.sx?sfzhm=%s", sfzh);
            System.out.println("=======================cl======================" + cl);
            String clResp = HttpClientUtil.doGet(cl);
            System.out.println("=======================clResp======================" + clResp);
            //String clResp = "[{\"CJH\":\"LSJDA11BX3D099930\",\"NJRQ\":\"20150331\",\"CPH\":\"苏ESN372\",\"LXFS\":\"13180602617\"},{\"CJH\":\"DDDDA11BX3D099930\",\"NJRQ\":\"20150331\",\"CPH\":\"苏ESN372\",\"LXFS\":\"13180602617\"}]";

            String jsz = String.format("http://10.35.252.127:8080/zhkclrytj/jdcxxcx/getXxBySfz.sx?sfzhm=%s", sfzh);
            System.out.println("=======================jsz======================" + jsz);
            String jszResp = HttpClientUtil.doGet(jsz);
            System.out.println("=======================jszResp======================" + jszResp);
            //String jszResp = "[{\"JSZH\":\"320520197903100416\",\"DABH\":\"320500417242\",\"XM\":\"龚懿鸣\"}]";

            //删除原有记录
            driverInfoApiMapper.removeDriverBind(sfzh);

            //车辆信息
            if (StringUtils.isNotEmpty(clResp) && clResp.length() > 10) {
                JSONArray arr1 = JSON.parseArray(clResp);
                for (int i = 0; i < arr1.size(); i++) {
                    JSONObject obj = arr1.getJSONObject(i);
                    DriverInfoApi dia = new DriverInfoApi();
                    dia.setCreateTime(new Date());
                    dia.setSfzh(sfzh);
                    dia.setVin(obj.getString("CJH"));//车架号
                    dia.setLpn(obj.getString("CPH"));//车牌号
                    //obj.getString("LXFS");//联系方式
                    if (null != obj.get("NJRQ")) {
                        try {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                            dia.setAid(sdf.parse(obj.getString("NJRQ")));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    driverInfoApiMapper.saveDriverInfo(dia);
                }
            }

            //驾驶证
            if (StringUtils.isNotEmpty(jszResp) && jszResp.length() > 10) {
                JSONArray arr2 = JSON.parseArray(jszResp);
                for (int i = 0; i < arr2.size(); i++) {
                    JSONObject obj = arr2.getJSONObject(i);
                    DriverInfoApi dia = new DriverInfoApi();
                    dia.setCreateTime(new Date());
                    dia.setSfzh(sfzh);
                    dia.setDrivingLicense(obj.getString("JSZH"));
                    dia.setFn(obj.getString("DABH"));
                    //obj.getString("XM");//姓名
                    driverInfoApiMapper.saveDriverInfo(dia);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "驾驶证信息保存失败");
        }
        return new Result(Result.SUCC, "驾驶证信息保存成功");
    }
}
